/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Account.java
 *
 * Created on 15.05.2009, 14:07:59
 */

package Tabs;

import Binding.Binder;
import Binding.RequiredRule;
import BusinessLayer.BusinessLayer;
import dialogs.ChangePassword;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.text.html.HTMLEditorKit.Parser;
import logging.Logger;
import wisi.WiSiApp;

/**
 *
 * @author benji
 */
public class Account extends javax.swing.JPanel {

    private Binder binder = new Binder();
    /** Creates new form Account */
    public Account() {
        initComponents();

        Logger.debug("Lade " + this.getName());
    }

    /**
     * Bindet die Daten von der Datenbank in die Anzeige für den USer
     */
    public void BindTo() {
        try{
            //if(!BusinessLayer.isLoggedIn())return;

            binder.BindTo(nameTextField, BusinessLayer.getUser(BusinessLayer.getUserId()));
            Logger.debug("Binde - Name");
            binder.BindTo(guthabenTextField, Integer.toString(BusinessLayer.getGuthaben(BusinessLayer.getUserId())));
            Logger.debug("Binde - Guthabe");
            binder.BindTo(rankLabel, BusinessLayer.getRank(BusinessLayer.getUserId()));
            Logger.debug("Binde - Rang");
            binder.BindTo(reportingTextField1,Integer.toString(BusinessLayer.getUmsatzProTag()));
            Logger.debug("Binde - Reporting");
            binder.BindTo(registerDatumLabel, BusinessLayer.getRegisterDate());
            Logger.debug("Binde - REgisterDate");

        }
        catch(Exception e){
            Logger.error("Bind To - Account Info", e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        guthabenTextField = new javax.swing.JTextField();
        reportingTextField1 = new javax.swing.JTextField();
        changePasswordButton = new javax.swing.JButton();
        changeNameButton = new javax.swing.JButton();
        registerDatumLabel = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        rankLabel = new javax.swing.JLabel();
        jButtonSaveAsXML = new javax.swing.JButton();
        jButtonLoadFromXML = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wisi.WiSiApp.class).getContext().getResourceMap(Account.class);
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel.setText(resourceMap.getString("jLabel.text")); // NOI18N
        jLabel.setName("jLabel"); // NOI18N

        nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        guthabenTextField.setEditable(false);
        guthabenTextField.setText(resourceMap.getString("guthabenTextField.text")); // NOI18N
        guthabenTextField.setName("guthabenTextField"); // NOI18N

        reportingTextField1.setEditable(false);
        reportingTextField1.setText(resourceMap.getString("reportingTextField1.text")); // NOI18N
        reportingTextField1.setName("reportingTextField1"); // NOI18N

        changePasswordButton.setText(resourceMap.getString("changePasswordButton.text")); // NOI18N
        changePasswordButton.setName("changePasswordButton"); // NOI18N
        changePasswordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changePasswordButtonActionPerformed(evt);
            }
        });

        changeNameButton.setText(resourceMap.getString("changeNameButton.text")); // NOI18N
        changeNameButton.setName("changeNameButton"); // NOI18N
        changeNameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeNameButtonActionPerformed(evt);
            }
        });

        registerDatumLabel.setText(resourceMap.getString("registerDatumLabel.text")); // NOI18N
        registerDatumLabel.setName("registerDatumLabel"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        rankLabel.setText(resourceMap.getString("rankLabel.text")); // NOI18N
        rankLabel.setName("rankLabel"); // NOI18N

        jButtonSaveAsXML.setText(resourceMap.getString("jButtonSaveAsXML.text")); // NOI18N
        jButtonSaveAsXML.setName("jButtonSaveAsXML"); // NOI18N
        jButtonSaveAsXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveAsXMLActionPerformed(evt);
            }
        });

        jButtonLoadFromXML.setText(resourceMap.getString("jButtonLoadFromXML.text")); // NOI18N
        jButtonLoadFromXML.setName("jButtonLoadFromXML"); // NOI18N
        jButtonLoadFromXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoadFromXMLActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel9)
                            .addContainerGap(402, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(changeNameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap())
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(reportingTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                                        .addComponent(guthabenTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                                        .addComponent(rankLabel)
                                        .addComponent(changePasswordButton, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(112, 112, 112))))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(registerDatumLabel)
                            .addContainerGap(263, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonLoadFromXML)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSaveAsXML)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(changeNameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(changePasswordButton, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(rankLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(guthabenTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel)
                    .addComponent(reportingTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(registerDatumLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSaveAsXML)
                    .addComponent(jButtonLoadFromXML))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Der user kann seinen Namen ändern
     * er schreibt den grwünschten namen in die dafür vorgesehene Zeile und klickt auf ändern um diesen auch zu speichern
     * @param evt
     */
    private void changeNameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeNameButtonActionPerformed
        binder.clearErrorList(); // alte Fehlerliste löschen
        String name;
        RequiredRule rRule = new RequiredRule();

        name = binder.BindFromString(nameTextField, rRule);

        //hat sich der name geändert
        if(name.equals(BusinessLayer.getUser(BusinessLayer.getUserId()))){
            Logger.user("Der Name hat sich nicht geändert");
            Logger.debug("Name wird nicht geändert weil er gleich geblieben ist");
        }
        else{
            //usernamen ändern
            if(!binder.getHasErrors()){
                BusinessLayer.changeUsername(name, BusinessLayer.getUserId());
                Logger.info("Username geändert in "+ name);
            }

        }


    }//GEN-LAST:event_changeNameButtonActionPerformed

    private void changePasswordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changePasswordButtonActionPerformed
        ChangePassword cp = new ChangePassword(WiSiApp.getApplication().getMainFrame(), true);
        cp.setVisible(true);
    }//GEN-LAST:event_changePasswordButtonActionPerformed

    private void jButtonSaveAsXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveAsXMLActionPerformed
        BusinessLayer.saveAsXML();
    }//GEN-LAST:event_jButtonSaveAsXMLActionPerformed

    private void jButtonLoadFromXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadFromXMLActionPerformed
        File sf = null;
        JFileChooser fc = new JFileChooser();
        int option = fc.showOpenDialog(Account.this);
         if (option == JFileChooser.APPROVE_OPTION) {
          sf = new File(fc.getSelectedFile().toString());
          Logger.info("File chosen: "+sf.getAbsolutePath());
        }
        else {
          Logger.info("Filechooser abort");
        }

        BusinessLayer.parseXML(sf);
        BusinessLayer.refresh();
    }//GEN-LAST:event_jButtonLoadFromXMLActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton changeNameButton;
    private javax.swing.JButton changePasswordButton;
    private javax.swing.JTextField guthabenTextField;
    private javax.swing.JButton jButtonLoadFromXML;
    private javax.swing.JButton jButtonSaveAsXML;
    private javax.swing.JLabel jLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel rankLabel;
    private javax.swing.JLabel registerDatumLabel;
    private javax.swing.JTextField reportingTextField1;
    // End of variables declaration//GEN-END:variables

}
